package com.swu.education.controller;

import com.swu.education.entity.Assignment;
import com.swu.education.service.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by nana on 2018/3/13.
 */

@RestController
public class AssignmentController {

  @Autowired
  AssignmentService assignmentService;

  //根据assignmentId查询当前的assignment的相关信息
  @RequestMapping(value = "/getAssignmentById")
  public Assignment getAssignmentById(@RequestParam(value = "assignmentId") int assignmentId){
    return assignmentService.getSingleAssignmentById(assignmentId);
  }


  //根据课程id 查询当前课程下的所有assignment
  @RequestMapping(value = "/getAllAssignment")
  public List<Assignment> getAllAssignment(HttpServletRequest request) throws Exception {
    int courseId = Integer.parseInt(request.getParameter("courseId"));
    return assignmentService.getAssignmentById(courseId);
  }

  //创建assignment
  @RequestMapping(value = "/createAssignment")
  public Assignment createAssignment(HttpServletRequest request){
    int courseId = Integer.parseInt(request.getParameter("courseId"));
    String assignmentName = request.getParameter("assignmentName");
    String assignmentDescribe = request.getParameter("description");
    String startTime = request.getParameter("startTime");
    String endTime = request.getParameter("endTime");
    return assignmentService.createAssignmentByCourseId(courseId,assignmentName,
            assignmentDescribe,startTime,endTime);
  }

  //删除assignment
  @RequestMapping(value = "/deleteAssignment")
  public void deleteAssignment(@RequestParam(value = "assignmentId") int assignmentId){
    assignmentService.deleteAssignmentById(assignmentId);
  }

  //修改课程信息
  @RequestMapping(value = "/modifyAssignment")
  public void modifyAssignment(HttpServletRequest request){
    int assignmentId = Integer.parseInt(request.getParameter("assignmentId"));
    String assignmentName = request.getParameter("assignmentName");
    String description = request.getParameter("description");
    String startTime = request.getParameter("startTime");
    String endTime = request.getParameter("endTime");
    assignmentService.modifyAssignmentInfoById(assignmentId,assignmentName,
            description,startTime,endTime);
  }
}
